<template>
  <el-drawer
    :close-on-press-escape="false"
    v-model="drawerVisible"
    size="1200px"
    :show-close="false"
    @close="handleClose"
  >
    <template #header>
      <div class="drawer-header">
        <div class="header-content">
          <span class="drawer-title">{{
            isEdit ? "修改角色" : "新增角色"
          }}</span>
        </div>
        <el-button type="text" @click="handleClose" class="close-btn"
          >关闭</el-button
        >
      </div>
    </template>

    <div class="form-container">
      <el-form
        :model="formData"
        :rules="formRules"
        ref="formRef"
        label-width="100px"
        class="role-form"
      >
        <el-form-item label="角色名称" prop="name">
          <el-input
            v-model="formData.name"
            placeholder="请输入角色名称"
            style="width: 400px"
          />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input
            type="textarea"
            v-model="formData.description"
            placeholder="请输入角色描述"
            :rows="4"
            style="width: 400px"
          />
        </el-form-item>
      </el-form>
    </div>

    <template #footer>
      <el-button plain @click="handleClose" class="footer-btn">取消</el-button>
      <el-button type="primary" @click="handleSave" class="footer-btn"
        >保存</el-button
      >
    </template>
  </el-drawer>
</template>

<script setup>
import { ref, reactive } from "vue";
import { addRole, updateRole } from "@/api";
import { ElMessage } from "element-plus";

const drawerVisible = ref(false);
const formRef = ref(null);
const isEdit = ref(false);
const currentRow = ref(null);

const formData = reactive({
  id: null,
  name: "",
  description: "",
});

const formRules = {
  name: [{ required: true, message: "请输入角色名称", trigger: "blur" }],
  description: [{ required: true, message: "请输入角色描述", trigger: "blur" }],
};

const emit = defineEmits(["refresh"]);

defineOptions({
  name: "RoleForm",
});

// 新增角色
const openAddDrawer = () => {
  isEdit.value = false;
  resetForm();
  drawerVisible.value = true;
};

// 修改角色
const openEditDrawer = (row) => {
  isEdit.value = true;
  currentRow.value = row;
  // 填充表单数据
  formData.id = row.id;
  formData.name = row.name;
  formData.description = row.description;
  drawerVisible.value = true;
};

// 重置表单
const resetForm = () => {
  formData.id = null;
  formData.name = "";
  formData.description = "";
  if (formRef.value) {
    formRef.value.resetFields();
  }
};

const handleClose = () => {
  drawerVisible.value = false;
  resetForm();
};

const handleSave = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      if (isEdit.value) {
        // 修改角色
        const updateData = {
          id: formData.id,
          name: formData.name,
          description: formData.description,
        };
        updateRole(updateData).then((res) => {
          if (res.code == 0) {
            ElMessage.success("修改成功");
            handleClose();
            emit("refresh");
          }
        });
      } else {
        // 新增角色
        addRole(formData).then((res) => {
          if (res.code == 0) {
            ElMessage.success("新增成功");
            handleClose();
            emit("refresh");
          }
        });
      }
    }
  });
};

defineExpose({
  openAddDrawer,
  openEditDrawer,
});
</script>

<style scoped lang="scss">
.drawer-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  padding: 16px 20px;

  .header-content {
    display: flex;
    align-items: center;
    gap: 20px;
  }

  .drawer-title {
    font-size: 18px;
    font-weight: 600;
    color: #333;
  }

  .close-btn {
    font-size: 16px;
    padding: 13px 16px;
    border: 1px solid #595757;
    color: #000000;
    background: #fff;
    border-radius: 4px;

    &:hover {
      color: #409eff;
      border-color: #409eff;
    }
  }
}

.form-container {
  padding: 20px;

  .role-form {
    max-width: 600px;
  }
}

.footer-btn {
  padding: 13px 16px !important;
  border: 1px solid #595757 !important;
}

// 确保抽屉内容区域撑满全屏
:deep(.el-drawer__body) {
  padding: 0;
  height: calc(100vh - 60px);
  overflow: hidden;
}

:deep(.el-drawer) {
  width: 100% !important;
}

:deep(.el-drawer__wrapper) {
  width: 100% !important;
}
</style>
